package com.appkubes.mall.restful.application.system.ad.controller;

import com.appkubes.base.core.entity.vo.AppKeyVO;
import com.appkubes.common.Response;
import com.appkubes.mall.core.entity.MallAd;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.system.ad.model.AdModel;
import com.appkubes.mall.restful.application.system.ad.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("广告模块")
@RestController
@RequestMapping("/ad")
public class AdController extends BaseController {


    @Autowired
    AdService adService;

    @ApiOperation(value = "获取广告板")
    @PostMapping("/getAds")
    public Object getAds(@RequestBody @Valid AppKeyVO appKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.error("参数检查失败");
        }


        Map<String, Object> response = new HashMap<>();
        List<AdModel> banner = new ArrayList<>();
        List<AdModel> goodBanner = new ArrayList<>();
        AdModel startupAd = null;
        AdModel indexAd = null;
        List<MallAd> data = adService.queryAll(appKeyVO);

        for (int i = 0; i < data.size(); i++) {
            AdModel ad = new AdModel();
            ad.setAdKey(data.get(i).getAdKey());
            ad.setTitle(data.get(i).getTitle());
            ad.setType(data.get(i).getPath());
            ad.setParam(data.get(i).getParam());
            ad.setPic(data.get(i).getPic());
            switch (data.get(i).getType()) {
                case 1:
                    banner.add(ad);
                    break;
                case 6:
                    if (startupAd == null) {
                        startupAd = ad;
                    }
                    break;
                case 8:
                    goodBanner.add(ad);
                    break;
                case 9:
                    if (indexAd == null) {
                        indexAd = ad;
                    }
            }

        }
        response.put("banner", banner);
        response.put("startupAd", startupAd);
        response.put("goodBanner", goodBanner);
        response.put("indexAd", indexAd);
        return imgResponse(response);
    }

}
